local UIBaptismResult       = UIBase.New(PanelResNames.UIBaptismResult)

local m_old_info            = nil

function UIBaptismResult.Init(data)
    m_old_info = data

    UIBase.Init(UIBaptismResult)
end

function UIBaptismResult:OnCreate(obj)
    self:RegisterOnClick(self:GetChild("bgClose"), self.OnClick_close)
    
    self.item           = self:GetChild("item")
    self.new_skill      = self:GetChild("skill2/textSkillType")
    self.old_skill      = self:GetChild("skill/textSkillType")

    UIBaptismResult:Refresh()
end

function UIBaptismResult:RefreshItem(chip)
    local item = CtrlManager.GetCtrl(PanelNames.Item);
    if item ~= nil then
        local ctr_debris = self.item:GetController("debris")
        local it_debris = self.item:GetChild("debris")
        ctr_debris.selectedIndex = chip.partnerId and 1 or 0
        it_debris:GetController("owner").selectedIndex = chip.partnerId and 1 or 0
        it_debris:GetController("lock").selectedIndex = chip.lock == 1 and 1 or 0

        item.initData(self.item, chip, 1, false, nil, chip.partnerId)
        -- item.name = tostring(cur_list[index + 1].itemModelId)
        local _heroData = HeroManager.getHeroDatabyPartnerId(chip["partnerId"],true)
        if _heroData then
            local url = "ui://ICONAtlas/".._heroData.icon
            it_debris:GetChild("hero"):GetChild("HeadIcon").url = url
        end

        self.item.onClick:Clear()
    end
end

function UIBaptismResult:Refresh()
    if m_old_info then
        local chip = StrangeFragmentManager.GetFragmentByItemId(m_old_info.itemId)

        UIBaptismResult:RefreshItem(chip)

        local old_skills = m_old_info.subSkills
        local sk = nil
        local sk_old = nil

        for i=1, #chip.subSkills do
            sk = chip.subSkills[i]
            for j=1, #old_skills do
                sk_old = old_skills[j]
                if sk.index == sk_old.index and sk.skillId ~= sk_old.skillId then
                    break
                end
            end
        end

        if #old_skills == 1 then
            sk_old = old_skills[1]
        end

        if G.dataTable["t_skill"][tostring(sk.skillId)] then
            self.new_skill.text = getLanguage(G.dataTable["t_skill"][tostring(sk.skillId)].f_Desc)
        end

        if G.dataTable["t_skill"][tostring(sk_old.skillId)] then
            self.old_skill.text = getLanguage(G.dataTable["t_skill"][tostring(sk_old.skillId)].f_Desc)
        end
    end
end

function UIBaptismResult:OnClick_close()
    closeUI(PanelNames.UIBaptismResult)
end

return UIBaptismResult